import fetcher from "../apis/Fetcher";




export type Authority = keyof { [I in (typeof authorities)[number]] }
export const authorities = [
    "人员_查询",
    "人员_新增",
    "人员_删除",
    "人员_角色_查询",
    "人员_角色_删除",
    "人员_角色_新增",
    "人员_角色_修改名称",
    "人员_角色_设置权限",
    "货品_查询",
    "货品_进价_查看",
    "货品_进价_修改",
    "人员_修改",
    "货品_新增",
    "货品_修改",
    "货品_删除",
    "货品_导入",
    "货品_分类_查询",
    "货品_分类_新增",
    "货品_分类_修改",
    "货品_分类_删除",
    "货品_品牌_查询",
    "货品_品牌_新增",
    "货品_品牌_修改",
    "货品_品牌_删除",
    "货品_品牌分组_查询",
    "货品_品牌分组_新增",
    "货品_品牌分组_修改",
    "卖场_查询",
    "卖场_查询所有",
    "卖场_新增",
    "卖场_修改",
    "卖场_户头_查询",
    "卖场_户头_新增",
    "卖场_户头_修改",
    "卖场_订单_查询",
    "卖场_订单_下单",
    "卖场_订单_删除",
    "卖场_订单_查询所有",
    "卖场_订单_添加货品",
    "卖场_订单_转化为送货单",
    "卖场_退货单_查询所有",
    "卖场_退货单_下单",
    "卖场_退货单_查询",
    "卖场_退货单_修改",
    "卖场_退货单_验收",
    "卖场_退货单_删除",
    "卖场_送货单_查询",
    "卖场_送货单_查询所有",
    "卖场_送货单_修改",
    "卖场_送货单_验收",
    "卖场_送货单_审核",
    "卖场_销售单_查询",
    "卖场_销售单_查询所有",
    "卖场_销售单_开单",
    "卖场_销售单_修改",
    "卖场_销售单_审核",
    "卖场_销售单_删除",
    "货品_厂商_查询",
    "货品_厂商_新增",
    "货品_厂商_修改",
    "人员_操作记录_查询",
    "库存_盘点_查询",
    "库存_盘点_新增",
    "库存_盘点_盘点",
    "库存_盘点_删除",
    "经销_客户_新增",
    "经销_客户_查询",
    "经销_客户_修改",
    "经销_客户_删除",
    "经销_查询",
    "经销_开单",
    "经销_修改",
    "经销_收款",
    "采购_查询",
    "采购_修改",
    "采购_下单",
    "采购_删除",
    "采购_供货商_查询",
    "采购_供货商_新增",
    "采购_供货商_修改",
    "采购_入库_查询",
    "采购_入库_修改",
    "采购_入库_入库",
    "采购_入库_验收",
    "货品_草稿单",
    "卖场_盘点_查询",
    "卖场_盘点_盘点",
    "卖场_盘点_删除",
    "采购_退货_查询",
    "采购_退货_修改",
    "采购_退货_创建",
] as const;


export interface AuthorityTreeItem {
    title: string
    children: AuthorityTreeItem[]
    key: string
}

export const authorityTree = (() => {
    const tree: AuthorityTreeItem[] = []
    authorities.map(authority => authority.split("_"))
        .forEach((steps) => {
            let currentFind = tree;
            steps.forEach((step, index) => {
                if (!currentFind.some(it => it.title === step)) {
                    currentFind.push({
                        title: step,
                        children: [],
                        key: steps.filter((_, index1) => index1 <= index).join("_")
                    })
                }
                currentFind = currentFind.find(it => it.title === step)?.children!
            })
        })
    return tree
})()


export function getRoles() {
    return fetcher.get<RoleResponse[]>("/roles")
}

export interface RoleResponse {
    id: number;
    name: string
    authorities: Authority[]
    employeeCount: number
}

export interface AddRoleRequest {
    name: string
}

export function addRole(name: string) {
    return fetcher.post<AddRoleResponse>("roles", {}, {name})
}

export interface AddRoleResponse {
    id: number
}

export function updateRoleName(name: string, newName: string) {
    return fetcher.patch(`roles/${name}/name`, {}, {name: newName})
}

export function deleteRole(name: String) {
    return fetcher.delete(`/roles/${name}`)
}

export function updateRoleAuthorities(name: string, authorities: Authority[]) {
    return fetcher.patch(`/roles/${name}/authorities`, {}, authorities)
}
